<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="地区" prop="region">
        <el-select v-model="queryParams.region" placeholder="地区（省份）" clearable>
          <el-option v-for="(city, index) in cityList" :key="index" :label="city" :value="city" />
        </el-select>
      </el-form-item>
      <el-form-item label="案件类型" prop="classifyName">
        <el-select v-model="queryParams.classifyName" placeholder="案件类型" clearable>
          <el-option v-for="(type, index) in typeList" :key="index" :label="type" :value="type" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="分类名" prop="classifyName">
        <el-input
          v-model="queryParams.classifyName"
          placeholder="请输入分类名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审理法院" prop="court">
        <el-input v-model="queryParams.court" placeholder="请输入审理法院" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="案件号" prop="caseNumber">
        <el-input v-model="queryParams.caseNumber" placeholder="请输入案件号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="案由 " prop="brief">
        <el-input v-model="queryParams.brief" placeholder="请输入案由 " clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="裁判日期" prop="refereeDate">
        <el-date-picker v-model="queryParams.refereeDate" clearable type="date" value-format="yyyy-MM-dd" placeholder="请选择裁判日期" />
      </el-form-item>
      <el-form-item label="更新时间" prop="deleteTime">
        <el-date-picker v-model="queryParams.deleteTime" clearable type="date" value-format="yyyy-MM-dd" placeholder="请选择更新时间" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:information:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:information:edit']" type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:information:remove']" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:information:export']" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="150" label="id" align="center" prop="id" />
      <el-table-column width="80" label="地区" align="center" prop="region" />
      <el-table-column width="100" label="案件类型" align="center" prop="classifyName" />
      <el-table-column width="300" label="标题" align="center" prop="title" />
      <!-- <el-table-column label="内容" align="center" prop="content" /> -->
      <el-table-column width="120" label="浏览次数" align="center" prop="viewed" />
      <el-table-column width="140" label="审理法院" align="center" prop="court" />
      <el-table-column width="200" label="案件号" align="center" prop="caseNumber" />
      <el-table-column width="150" label="案由" align="center" prop="brief" />
      <el-table-column label="裁判日期" align="center" prop="refereeDate" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refereeDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="deleteTime" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deleteTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        width="200"
        label="案件附件"
        align="center"
        prop="enclosure"
      />-->
      <el-table-column label="案件附件" align="center" prop="deleteTime" width="250">
        <template slot-scope="scopeB">
          <el-link :href="`${baseUrl}${scopeB.row.enclosure}`" :underline="false" target="_blank">
            <span class="el-icon-document">{{ getFileName(scopeB.row.enclosure) }}</span>
          </el-link>
          <!-- <span>{{ scopeB.row.enclosure }}</span> -->
        </template>
      </el-table-column>
      <!-- <file-upload v-model="form.enclosure" /> -->
      <el-table-column width="150" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['system:information:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-hasPermi="['system:information:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改案件信息详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="案件类型" prop="classifyName">
              <el-select v-model="form.classifyName" placeholder="案件类型" clearable>
                <el-option v-for="(type, index) in typeList" :key="index" :label="type" :value="type" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地区" prop="region">
              <el-select v-model="form.region" placeholder="地区（省份）" clearable>
                <el-option v-for="(city, index) in cityList" :key="index" :label="city" :value="city" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="裁判日期" prop="refereeDate">
              <el-date-picker v-model="form.refereeDate" clearable type="date" value-format="yyyy-MM-dd" placeholder="请选择裁判日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新时间" prop="deleteTime">
              <el-date-picker v-model="form.deleteTime" clearable type="date" value-format="yyyy-MM-dd" placeholder="请选择更新时间" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" />
          </el-form-item>
        </el-row>
        <el-form-item label="案件号" prop="caseNumber">
          <el-input v-model="form.caseNumber" placeholder="请输入案件号" />
        </el-form-item>
        <el-form-item label="审理法院" prop="court">
          <el-input v-model="form.court" placeholder="请输入审理法院" />
        </el-form-item>
        <el-form-item label="案由 " prop="brief">
          <el-input v-model="form.brief" placeholder="请输入案由 " />
        </el-form-item>

        <el-form-item label="浏览次数" prop="viewed">
          <el-input v-model.number="form.viewed" placeholder="请输入浏览次数" />
        </el-form-item>
        <el-form-item label="案件附件">
          <file-upload v-model="form.enclosure" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInformation,
  getInformation,
  delInformation,
  addInformation,
  updateInformation
} from '@/api/system/information'
import { listClassify } from '@/api/system/classify'
export default {
  name: 'Information',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 案件信息详情表格数据
      informationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        region: null,
        classifyId: null,
        classifyName: null,
        title: null,
        content: null,
        viewed: null,
        court: null,
        caseNumber: null,
        brief: null,
        refereeDate: null,
        deleteTime: null,
        enclosure: null
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 30
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      typeList: null,
      cityList: [
        '河北',
        '山西',
        '辽宁',
        '吉林',
        '黑龙江',
        '江苏',
        '浙江',
        '安徽',
        '福建',
        '江西',
        '山东',
        '河南',
        '湖北',
        '湖南',
        '广东',
        '海南',
        '四川',
        '贵州',
        '云南',
        '陕西',
        '甘肃',
        '青海',
        '台湾',
        '内蒙古',
        '广西',
        '西藏',
        '宁夏',
        '新疆',
        '北京',
        '天津',
        '上海',
        '重庆',
        '香港',
        '澳门'
      ],
      baseUrl: process.env.VUE_APP_BASE_API
    }
  },
  created() {
    this.getTypeList()
    this.getList()
  },
  methods: {
    // 获取文件名称
    getFileName(name) {
      if (name.lastIndexOf('/') > -1) {
        return name.slice(name.lastIndexOf('/') + 1)
      } else {
        return ''
      }
    },
    getTypeList() {
      listClassify().then(res => {
        const data = res.rows
        this.typeList = data.map(item => {
          return item.name
        })
      })
    },
    /** 查询案件信息详情列表 */
    getList() {
      this.loading = true
      listInformation(this.queryParams).then(response => {
        this.informationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        region: null,
        classifyId: null,
        classifyName: null,
        title: null,
        content: null,
        viewed: null,
        court: null,
        caseNumber: null,
        brief: null,
        refereeDate: null,
        createTime: null,
        updateTime: null,
        deleteTime: null,
        enclosure: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加案件信息详情'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInformation(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改案件信息详情'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addInformation(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除案件信息详情编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInformation(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/information/export',
        {
          ...this.queryParams
        },
        `information_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
